import { Graph, Addon } from '@antv/x6'
import { toolbarWidth } from '@/config/index'
import { stencilNodeOptions } from './config'

/**
 * 创建模板工具
 * @param graph 画布
 * @returns
 */
export function createStencilTool(graph: Graph): Addon.Stencil {
  const groups = stencilNodeOptions.map((e) => e.group)
  const stencil = new Addon.Stencil({
    title: '常用节点',
    target: graph,
    stencilGraphWidth: toolbarWidth - 10,
    collapsable: true,
    groups
  })
  const stencilContainer = document.getElementById('stencil')!
  stencilContainer.appendChild(stencil.container)
  // 添加工具节点
  for (let item of stencilNodeOptions) {
    const nodes = item.node.map((e) => graph.createNode(e))
    stencil.load(nodes, item.group.name)
  }
  return stencil
}
